$(function () {
    /*
     * 咨询者姓名获取焦点
     */
    $("#consultant_name").focus();

    /*
     * 在咨询者列表展示更多操作
     */
    $(".consultant_more").on("click", function () {
        $(this).next().animate({
            width: "90px",
            right: "10px"
        }, 300);
    })

    $(".consultant_list tbody tr").on("mouseleave", function () {
        $(".consultant_btn").animate({
            width: "0",
            right: "0"
        }, 200)
    })

    /*
     * 判断咨询者是否重复
     */
    var is_change = true;//判断是否要检测用户是否重复
    $("#consultant_phone").on("change", function () {
        is_change = true;
    });
    $("#consultant_phone").on("blur", function () {
        if (is_change) {
            is_change = false;
            var now_add_phone = $("#consultant_phone").val();
            var data = {
                "now_add_phone": now_add_phone
            };
            var request_url = package_url("consultant_module/index/check_info");
            crm_ajax(data, request_url, function (res) {
                if (res.length>0) {
                    crm.alert("电话号码已存在,咨询者是:" + res[0].name + ",咨询师是:" + res[0].staff_name, "error", function () {
                        $("#consultant_phone").focus();
                    })
                }
            });
        }
    })

    /*
     * 展示咨询者记录
     */
    $(".record_open").on("click", function () {
        var now_consultant = $(this).parent().parent().attr("data-id");
        var now_name = $(this).parent().parent().attr("data-name");
        var data = {
            "now_consultant": now_consultant,
            "now_name": now_name
        };
        var request_url = package_url("consultant_module/index/show_record");
        show_record(data, request_url, now_name, now_consultant);
    })

    /*
     * 展示咨询者记录弹窗
     */
    function show_record(data, request_url, now_name, now_consultant) {
        crm_ajax(data, request_url, function (res) {
            var mid_content = "", pre_popup_html = "", after_popup_html = "", intention_html = "";
            $(".popup_rich_text").siblings().remove();
            $(".popup_rich_text").css("display", "none");
            $("#editor").removeClass();
            $("#editor").addClass("form-control rich_text");
            if (res.record.length > 0) {
                for (k in res.record) {
                    mid_content += "<div class='consultant_record_content'><div><span>[" + res.record[k].time + "]</span><span>(咨询师:" + res.record[k].name + ")</span></div><div>" + res.record[k].content + "</div></div>";
                }
            } else {
                mid_content += "<h4 style='text-align: center;'>记录为空</h4>";
            }
            var pre_popup_html = "<div class='row wrapper popup_content'>" + mid_content + "</div>";
            for (j in res.intention_list) {
                if (j == (res.consultant_list.intention_id - 1)) {
                    intention_html += "<option value=" + (parseInt(j) + 1) + " selected>" + res.intention_list[j].type + "</option>";
                } else {
                    intention_html += "<option value=" + (parseInt(j) + 1) + ">" + res.intention_list[j].type + "</option>";
                }
            }
            var after_popup_html = "<div class='popup_bottom_content consultant_record_bottom my_conditions'><span>上门状态:</span><select name='visit_state' id='visit_state'><option value='1' " + (res.consultant_list.visit_state == 1 ? "selected" : "") + ">已上门</option><option value='0' " + (res.consultant_list.visit_state == 0 ? "selected" : "") + ">未上门</option></select><span>意向情况:</span><select name='intention_id' id='intention_id'>" + intention_html + "</select><span>联系状态:</span><select name='contact_state' id='contact_state'><option value='1' " + (res.consultant_list.contact_state == 1 ? "selected" : "") + ">已联系</option><option value='0' " + (res.consultant_list.contact_state == 0 ? "selected" : "") + ">未联系</option></select></div><input type='submit' class='btn btn-s-md btn-default confirm_record' data-id='" + now_consultant + "' data-name='" + now_name + "' value='提 交'>";
            popup_window(now_name + "的记录", pre_popup_html, after_popup_html, function () {
                $(".popup_rich_text").css("display", "block");
                $(".confirm_record").on("click", confirm_consultant_record);
                $(".popup_exit").addClass("record_exit")
                $(".record_exit").on("click", function () {
                    location.href = window.location.href;
                })
            });
        })
    }

    /*
     * 添加咨询者记录
     */
    function confirm_consultant_record() {
        var record_info = $.trim($(".rich_text").html());
        var now_consultant = $(this).attr("data-id");
        var now_name = $(this).attr("data-name");
        var visit_state = $("#visit_state").find("option:selected").attr("value");
        var intention_id = $("#intention_id").find("option:selected").attr("value");
        var contact_state = $("#contact_state").find("option:selected").attr("value");
        if (record_info.length <= 0) {
            crm.alert("咨询记录不能为空", "error");
        } else {
            var data = {
                "record_content": record_info,
                "now_consultant": now_consultant,
                "visit_state": visit_state,
                "intention_id": intention_id,
                "contact_state": contact_state
            };
            var request_url = package_url("consultant_module/index/confirm_record");
            var error_back_url = package_url("consultant_module/index/show_list");
            if (record_info == "") {
                crm.alert("记录信息不能为空", "error");
            } else {
                crm_ajax(data, request_url, function (res) {
                    if (res.status == 1) {
                        $(".rich_text").html("");
                        $(".consultant_record_content").html("");
                        var data = {
                            "now_consultant": now_consultant,
                            "now_name": now_name
                        };
                        var request_url = package_url("consultant_module/index/show_record");
                        show_record(data, request_url, now_name, now_consultant);
                    } else {
                        crm.alert(res.msg, "error");
                    }
                }, error_back_url);
            }
        }
    }

    /*
     *展示咨询者详细信息
     */
    $(".consultant_show").on("click", function () {
        var now_consultant = $(this).parent().parent().parent().parent().parent().attr("data-id");
        var request_url = package_url("consultant_module/index/get_consultant_info");
        var data = {
            "now_consultant": now_consultant
        };
        crm_ajax(data, request_url, function (res) {
            var popup_html = "<div class='row wrapper info_content'><div class='col-sm-3 info_left'>姓名</div><div class='col-sm-9 info_right'>" + res[0].name + "</div><div class='col-sm-3 info_left'>电话号码</div><div class='col-sm-9 info_right'>" + res[0].phone + "</div><div class='col-sm-3 info_left'>性别</div><div class='col-sm-9 info_right'>" + (res[0].sex == 1 ? '男士' : '女士') + "</div><div class='col-sm-3 info_left'>学历</div><div class='col-sm-9 info_right'>" + res[0].grade + "</div><div class='col-sm-3 info_left'>学校</div><div class='col-sm-9 info_right'>" + (res[0].school ? res[0].school : 'NULL') + "</div><div class='col-sm-3 info_left'>微信</div><div class='col-sm-9 info_right'>" + (res[0].wechat ? res[0].wechat : 'NULL') + "</div><div class='col-sm-3 info_left'>QQ</div><div class='col-sm-9 info_right'>" + (res[0].qq ? res[0].qq : 'NULL') + "</div><div class='col-sm-3 info_left'>应聘职位</div><div class='col-sm-9 info_right'>" + (res[0].position ? res[0].position : 'NULL') + "</div><div class='col-sm-3 info_left'>所读专业</div><div class='col-sm-9 info_right'>" + (res[0].major ? res[0].major : 'NULL') + "</div><div class='col-sm-3 info_left'>上门情况</div><div class='col-sm-9 info_right'>" + (res[0].visit_state == 1 ? '上门' : '未上门') + "</div><div class='col-sm-3 info_left'>咨询者意向</div><div class='col-sm-9 info_right'>" + (res[0].intention ? res[0].intention : 'NULL') + "</div><div class='col-sm-3 info_left'>咨询渠道</div><div class='col-sm-9 info_right'>" + (res[0].channel ? res[0].channel : 'NULL') + "</div><div class='col-sm-3 info_left'>联系情况</div><div class='col-sm-9 info_right'>" + (res[0].contact_state == 1 ? '已联系' : '未联系') + "</div></div><div class='popup_bottom'><a href='/consultant_module/index/show_add_edit/now_consultant/" + now_consultant + "' class='btn btn-s-md btn-default'>修 改</a></div>";
            popup_window(res[0].name + "的个人信息", popup_html);
        })
    })

    /*
     * crm提醒弹窗展示
     */
    $(".message_open").on("click", function () {
        var now_name = $(this).parent().parent().parent().parent().parent().attr("data-name");
        var now_id = $(this).parent().parent().parent().parent().parent().attr("data-id");
        var per_popup_html = "<p style='text-align: right;'><input class='Wdate message_day' type='text'  name='message_day'></p>";
        var after_popup_html = "<input style='display: block; width: 94px; margin: 0 auto;' type='submit' class='btn btn-s-md btn-default confirm_message' data-id='" + now_id + "' data-name='" + now_name + "' value='提 交'>";
        popup_window("设置" + now_name + "的消息提醒", per_popup_html, after_popup_html, function () {
            $(".message_day").on("focus",function () {
                WdatePicker ({startDate:"%y-%M-01 00:00:00",dateFmt:"yyyy-MM-dd HH:mm:ss",alwaysUseStartDate:true})
            })
            $(".popup_rich_text").css("display", "block");
            $(".rich_text").addClass("message_info");
            var mydate = new Date();
            $(".message_day").val(mydate.toLocaleString());
            $(".confirm_message").on("click", confirm_message)
        });
    })

    /*
     * 提醒消息提交
     */
    function confirm_message() {
        var time = $.trim($(".message_day").val());
        var content = $.trim($(".message_info").html());
        var now_time = Date.parse(new Date()) / 1000;
        var time1 = get_time(time);
        if (time == "") {
            crm.alert("提醒消息时间格式错误", "error");
        } else if (time1 <= now_time) {
            crm.alert("提醒消息时间不能小于当前时间", "error");
        } else if (content.length <= 0) {
            crm.alert("提醒消息内容不能为空", "error");
        } else {
            var now_consultant = $(".confirm_message").attr("data-id");
            var now_name = $(".confirm_message").attr("data-name");
            var data = {
                "now_consultant": now_consultant,
                "now_time": time1,
                "content": content,
                "now_name": now_name
            }
            var request_url = package_url("consultant_module/message/add_message");
            crm_ajax(data, request_url, function (res) {
                if (res.status == 1) {
                    var type = "success";
                } else {
                    var type = "error";
                }
                crm.alert(res.msg, type, function () {
                    location.href = package_url("consultant_module/index/show_list");
                })
            }, package_url("consultant_module/index/show_list"));
        }
    }

    /*
     * 打开添加学员页面
     */
    $(".add_student").on("click", function () {
        var now_con_name = $(this).parent().parent().parent().parent().parent().attr("data-name");
        var now_con_id = $(this).parent().parent().parent().parent().parent().attr("data-id");
        var request_url = package_url("administration_module/index/show_add") + "/now_consultant/" + now_con_id;
        var error_back_url = package_url("consultant_module/index/show_list");
        var data = {
            "id": now_con_id
        };
        var text = "您确定将" + now_con_name + "设为学生么?"
        crm.alert(text, "question", function () {
            location.href = request_url;
        }, true);
    })


})